package stack

import (
	C "gitee.com/ljfirst/algo-go-sdk/common/constant"
)

/**
 * @author ljfirst
 * @version V1.0
 * @date 2023/6/27 01:09
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description
 * */
type ArrayStack struct {
	stack []interface{}
}

func (m *ArrayStack) Push(value interface{}) {
	m.stack = append(m.stack, value)
}

func (m *ArrayStack) Pop() interface{} {
	if len(m.stack) == 0 {
		return C.ErrorNum
	}
	length := len(m.stack) - 1
	value := m.stack[length]
	m.stack = m.stack[:length]
	return value
}

func (m *ArrayStack) Peak() interface{} {
	if len(m.stack) == 0 {
		return C.ErrorNum
	}
	return m.stack[len(m.stack)-1]
}

func (m *ArrayStack) Resize() {
	stackTemp := make([]interface{}, len(m.stack)*2)
	for k, v := range m.stack {
		stackTemp[k] = v
	}
	m.stack = stackTemp
}

func (m *ArrayStack) Empty() bool {
	return len(m.stack) == 0
}

func (m *ArrayStack) Size() int {
	return len(m.stack)
}

func (m *ArrayStack) GetAttribute() *C.Attribute {
	return &C.Attribute{
		Tags: []string{C.Stack},
		Desc: &C.Desc{
			Name:   "ArrayStack",
			NameCn: "数组栈",
			ParamsDesc: map[string]string{
				"stack": "内部维护一个数组来控制先进后出",
			},
		},
	}
}
